package Testclass.ajaxTest;

import Model.Stu;
import com.alibaba.fastjson.JSONObject;
import Dao.jdbcTools;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/ajax6")
public class ajax6 extends HttpServlet{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        resp.setContentType("application/json");
        req.setCharacterEncoding("utf-8");//响应用户请求时，告知用户服务器的编码
        String data = req.getParameter("data");
        System.out.println("data = " + data);
        JSONObject jsonObject = JSONObject.parseObject(data.toString());
        Integer id = Integer.valueOf( jsonObject.getString("id"));
        String name = jsonObject.getString("name");
        String sex = jsonObject.getString("sex");
        Integer age = Integer.valueOf( jsonObject.getString("age"));
        String tel = jsonObject.getString("tel");
        String scores = jsonObject.getString("scores");
        String school = jsonObject.getString("school");
        Stu stu = new Stu();
        stu.setId(id);stu.setName(name);
        stu.setSex(sex);stu.setAge(age);stu.setTel(tel);
        stu.setScores(scores);stu.setSchool(school);
        try {
            int add = jdbcTools.add("insert into students values(?,?,?,?,?,?,?)", id,name,sex,age,tel,scores,school);
            System.out.println(add);
        } catch (Exception e) {
            e.printStackTrace();
        }
        PrintWriter writer = resp.getWriter();
        writer.write("success");
    }
}
